require 'albacore'
require 'rake/clean'
require 'build/Configuration.rb'

Albacore::log_level = :verbose

REVISION = ENV["BUILD_VCS_NUMBER"]
BUILD_NUMBER = ENV['BUILD_NUMBER']
BUILD_ON = ENV['agent.name']


#Cleanup the output folder
CLEAN.include(OUTPUT_DIR)


task :default => ["clean", "build:all"]
namespace :build do
    desc "Run a complete build"
    task :all => [:init, :assemblyinfo, :compile, :test]
	
	desc "Initialisation"
	task :init do
	puts "Creating folders..."
		Dir.mkdir(OUTPUT_DIR)
		#Dir.mkdir(ARTIFACTS_PATH)
	
		puts "Deleting any existing bin folders..."
		FileUtils.rm_r FileList["#{SOURCE_DIR}/**/bin"], :force => true
		FileUtils.rm_r FileList["#{SOURCE_DIR}/**/obj"], :force => true
		
		puts "TeamCity 1: #{ENV['teamcity.dotnet.nunitlauncher']}"
	end
       
	

    desc "Run a sample assembly info generator"
    assemblyinfo :assemblyinfo do |asm|
		asm.version = "#{BUILD_NUMBER}"
         asm.file_version= "#{BUILD_NUMBER}"
         asm.description = "Revision: #{REVISION} \n BuiltOn: #{BUILD_ON}"
         asm.output_file = "AssemblyInfoVersion.cs"
    end

    desc "Build solution using MSBuild."
    msbuild :compile do |msb|
	   puts "Compiling Solution..."
       msb.properties :configuration => :Debug
       msb.targets :Clean, :Build

       #msb.parameters "/l:JetBrains.BuildServer.MSBuildLoggers.MSBuildLogger,C:/BuildServer/teamcity/buildagent/plugins/dotnetplugin/bin/JetBrains.BuildServer.MSBuildLoggers.dll"
       msb.solution = SOLUTION_FILE
    end
	   
	     desc "Runs tests with NUnit only (without coverage)."
		 task :test => [:compile] do
		 puts "Running Tests..."
		 tests = FileList["#{SOURCE_DIR}**/*.Tests.dll"].exclude(/obj\//)
			
			#Select the correct test runner
			if(TEAMCITY_NUNIT_RUNNER == nil)
				sh "#{NUNIT_EXE} #{tests} /nologo /exclude:Acceptance /xml=#{OUTPUT_DIR}/TestResults.xml"
			else
				sh "#{TEAMCITY_NUNIT_RUNNER} v2.0 x86 NUnit-2.4.6 /category-exclude:Acceptance #{tests}"
			end
		 end

       desc "Output all environment variables"
       task :outputVariable do
			puts 'Build number: ' + ENV['BUILD_NUMBER']
			ENV.each {|key, value| puts "#{key} = #{value}" }
       end
end